package com.example.shysx11.web;

import com.example.shysx11.pojo.Supervisor;       // 修改包名
import com.example.shysx11.service.ISupervisorService; // 修改包名
import com.example.shysx11.vos.ResultVo;           // 修改包名
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/supervisor")
@CrossOrigin(
        origins = "http://localhost:5173", // 前端地址，需与实际一致
        allowCredentials = "true",
        maxAge = 3600
)
public class SupervisorWeb {
    private final ISupervisorService service;

    // 构造器注入（推荐方式）
    public SupervisorWeb(ISupervisorService service) {
        this.service = service;
    }

    @PostMapping("/login")
    public ResultVo login(@RequestBody Supervisor supervisor) {
        System.out.println("登录请求：" + supervisor.getTelId());
        return service.getOne(supervisor.getTelId(), supervisor.getPassword());
    }

    @PostMapping("/register")
    public ResultVo register(@RequestBody Supervisor supervisor) {
        try {
            return service.register(supervisor);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("注册失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public ResultVo list() {
        return service.getSupervisors();
    }
}